<!DOCTYPE html>
<html>
<head>
    {{template "header"}}
</head>
<body>
<script>

    let getColumns = function () {
        return [
            {
                height: 30,
                cols: [
                    {view: "text", name: "name", label: tr("node","Name"), css: "nborder-input",},
                    {view: "text", name: "tags", label: gtr("Tags"), css: "nborder-input",},
                ]
            },
            {view: "textarea", name: "remark", label: gtr("Remark")},
        ]
    }

    let deleteItem = function (ids, callback) {
        webix.confirm({
            title: gtr("Operation confirmation"),
            ok: gtr("Yes"), cancel: gtr("No"),
            text: gtr("Confirm to delete? This operation is irreversible."),
            callback: function (ev) {
                if (ev) {
                    webix.ajax().get('/admin/node/delete', {ids: ids}).then(function (result) {
                        if (callback)
                            callback()
                    }).fail(function (xhr) {
                        webix.message({type: 'error', text: gtr("Delete Failure:") + xhr.statusText, expire: 2000});
                    });
                }
            }
        });
    }

    webix.ready(function () {
        let importUrl = '/admin/node/import'
        let exportUrl = '/admin/node/export'
        let tableid = webix.uid();
        let uploadid = webix.uid();
        let queryid = webix.uid()
        let reloadData = wxui.reloadDataFunc(tableid, "/admin/node/query", null)
        wxui.initUploadApi(uploadid, importUrl, reloadData);
        webix.ui({
            css: "main-panel",
            padding: 7,
            rows: [
                wxui.getPageToolbar({
                    title: tr("node","Node"),
                    icon: "mdi mdi-server",
                    elements: [
                        wxui.getPrimaryButton(gtr("Edit"), 90, false, function () {
                            let item = $$(tableid).getSelectedItem();
                            if (item) {
                                let vitem = webix.copy(item)
                                vitem.api_pwd = ""
                                wxui.openFormWindow({
                                    width: 640,
                                    height: 640,
                                    title: tr("node","Node edit"),
                                    data: vitem,
                                    post: "/admin/node/update",
                                    callback: reloadData,
                                    elements: getColumns()
                                }).show();
                            } else {
                                webix.message({type: 'error', text: "Please select one", expire: 1500});
                            }
                        }),
                        wxui.getPrimaryButton(gtr("Clone"), 90, false, function () {
                            let item = $$(tableid).getSelectedItem();
                            if (item) {
                                let vitem = webix.copy(item)
                                vitem.id = ""
                                wxui.openFormWindow({
                                    width: 640,
                                    height: 640,
                                    title: tr("node","Node clone"),
                                    data: vitem,
                                    post: "/admin/node/add",
                                    callback: reloadData,
                                    elements: getColumns()
                                }).show();
                            } else {
                                webix.message({type: 'error', text: "Please select one", expire: 1500});
                            }
                        }),
                        wxui.getPrimaryButton(gtr("Create"), 90, false, function () {
                            wxui.openFormWindow({
                                width: 640,
                                height: 640,
                                title: tr("node","Node create"),
                                post: "/admin/node/add",
                                callback: reloadData,
                                elements: getColumns()
                            }).show();
                        }),
                        wxui.getDangerButton(gtr("Remove"), 90, false, function () {
                            let rows = wxui.getTableCheckedIds(tableid);
                            if (rows.length === 0) {
                                webix.message({type: 'error', text: "Please select one", expire: 1500});
                            } else {
                                deleteItem(rows.join(","), reloadData);
                            }
                        }),
                    ]
                }),
                wxui.getDatatable({
                    tableid: tableid,
                    url: '/admin/node/query',
                    columns: [
                        {
                            id: "state",
                            header: {content: "masterCheckbox", css: "center"},
                            headermenu: false,
                            adjust: true,
                            css: "center", width: 45,
                            template: "{common.checkbox()}"
                        },
                        {
                            id: "name", header: [tr("node","Name")], adjust: true, sort: "server",
                            template: "<a class='do_detail' href='javascript:void(0)'>#name#</a>"
                        },
                        {id: "tags", header: [gtr("Tags")], adjust: true, sort: "server"},
                        {id: "remark", header: [gtr("Remark")], sort: "string", fillspace: true},
                        // {header: {content: "headerMenu"}, headermenu: false, width: 35}
                    ],
                    leftSplit: 1,
                    pager: true,
                    on: {},
                }),
                wxui.getTableFooterBar({
                    tableid: tableid,
                    actions: [
                        wxui.getIconButton(gtr("Import"), 90, "import", false, function () {
                            $$(uploadid).fileDialog({});
                        }),
                        wxui.getIconButton(gtr("Export"), 90, "download", false, function () {
                            wxui.exportData(exportUrl, 'nodes.csv')
                        }),
                    ],
                    callback: reloadData
                }),
            ]
        })
    })
</script>
</body>
</html>